package com.ajk.server.controller;

import java.security.Principal;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ajk.server.controller.access.ArgIndexForPrintLog;
import com.ajk.server.model.TestAnswer;
import com.ajk.server.model.TestCategoryDict;
import com.ajk.server.model.request.QuestionReq;
import com.ajk.server.model.response.ResultList;
import com.ajk.server.model.response.ResultObject;
import com.ajk.server.model.response.TestAnswerRes;
import com.ajk.server.model.response.TestCategoryDictRes;
import com.ajk.server.service.TestAnswerService;
import com.ajk.server.service.TestCategoryDictService;
import com.ajk.server.service.TestQuestionService;

@RequestMapping("/questionnaire")
@RestController
public class QuestionnaireController {
	
	private static Logger logger = LoggerFactory.getLogger(QuestionnaireController.class);
	
	@Autowired
	TestCategoryDictService testCategoryDictService;//健康自测类型
	
	@Autowired
	TestQuestionService testQuestionService;//测试题
	
	@Autowired
	TestAnswerService testAnswerService;//测试记录
	
	/**
	 * 加载试卷列表，
	 * @param parentId
	 * @param start
	 * @param limit ： 0表示加载所有
	 * @return
	 */
	@ArgIndexForPrintLog(indexes={0,1,2})
	@RequestMapping(value = "/fetch_paper_list/{parentId}", method = RequestMethod.GET)
	public ResultList<TestCategoryDict> fetchPaperList(
			@PathVariable("parentId") Integer parentId,
			Integer start,
			Integer limit,Principal principal){
		
		List<TestCategoryDict> list = testCategoryDictService.findByParentId(parentId, start, limit);
		
		
		return new ResultList<>(list);
	}
	
	/**
	 * 加载一个试卷的题目
	 * @param parentId
	 * @return
	 */
	@ArgIndexForPrintLog(indexes={0})
	@RequestMapping(value = "/fetch_paper/{paperId}", method = RequestMethod.GET)
	public ResultObject<TestCategoryDictRes> fetchPaper(@PathVariable("paperId") Integer paperId,Principal principal){
		
		TestCategoryDictRes res = testCategoryDictService.findPaperByIdWithCahche(paperId);
		
		return new ResultObject<TestCategoryDictRes>(res);
	}
	
	/**
	 * [
	 * 	{
	 * 		questionId:12,
	 * 		itemIds:[1,2,3]
	 *  }
	 * ]
	 * @param paperId
	 * @param principal
	 * @return
	 */
	@ArgIndexForPrintLog(indexes={0,1})
	@RequestMapping(value = "/patients/submit_paper/{paperId}", method = RequestMethod.POST,consumes = "application/json;chatset=UTF-8")
	public ResultObject<TestAnswer> submitPaper(@PathVariable("paperId") Integer paperId,@RequestBody QuestionReq question ,Principal principal){
		
		Integer userId = question.getUserId();
		if(null != principal){
			userId = Integer.parseInt(principal.getName());
		}
		TestAnswer res = testAnswerService.submitPaper(userId,paperId,question.getStationId(),question);
		return new ResultObject<TestAnswer>(res);
	}
	
	/**
	 * 获取我的答案
	 * @param parentId
	 * @return
	 */
	@ArgIndexForPrintLog(indexes={0})
	@RequestMapping(value = "/patients/fetch_answer_list", method = RequestMethod.GET)
	public ResultList<TestAnswerRes> fetchTestAnswer(Integer start,
			Integer limit,Principal principal){
		
		Integer userId = Integer.parseInt(principal.getName());
		
		List<TestAnswerRes> res = testAnswerService.findAnswer(userId,start,limit);
		
		return new ResultList<TestAnswerRes>(res);
	}
	
	/**
	 * 测试分享
	 */
	@ArgIndexForPrintLog(indexes={0})
	@RequestMapping(value = "/answer/{answerId}/{shareCategoryId}")
	public ResultObject<Integer> shareAnswer(Principal principal,@PathVariable("answerId") Integer answerId,@PathVariable("shareCategoryId") Integer shareCategoryId){
		
		Integer userId = Integer.parseInt(principal.getName());
		
		Integer id = testAnswerService.shareAnswer(answerId, shareCategoryId, userId);
		
		return new ResultObject<Integer>(id);
		
	}

}
